package com.wva.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 重定向 :
 *  测试:直接访问/demo01response
 */
@WebServlet("/demo01response")
public class Demo01Response extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo01response........");

        //访问/responseDemo1，会自动跳转到/responseDemo2资源

        /*
        // 重定向方法
        //1. 设置状态码为302
        response.setStatus(302);
        //2.设置响应头location
        response.setHeader("location","demo2response");
        */
        request.setAttribute("msg","response");
//        //动态获取虚拟目录
        String contextPath = request.getContextPath();
        System.out.println(contextPath); //19_tomcat

//        //简单的重定向方法
        response.sendRedirect(contextPath+"/demo2response");
//        //response.sendRedirect("http://www.itcast.cn");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
